function test() {
    class A {
        constructor() {
            if (A.instance) {
                return A.instance;
            }
            A.instance = this;
            this.name = 'A';
        }


        getName() {
            return this.name;
        }

        setName(name) {
            this.name = name;
        }
    }

    function getInstance() {
        if (!test.instance) {
            test.instance = new A();
        }
        return test.instance; 
    }

    return {
        getInstance
    }


    // if (!test.instance) {
    //     test.instance = new A();
    // }
    // return test.instance;

    // return new A();
}

const instance = test().getInstance();
instance.setName('New Name');
const instance2 = test().getInstance();
instance2.getName(); // 'New Name'

console.log(instance === instance2); // true, 因为 test() 返回的是同一个实例